package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.service.CourseService;
import com.lagou.service.impl.CourseServiceImpl;
import com.lagou.utils.DateUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.http.HttpRequest;
import java.util.List;
import java.util.Map;

@WebServlet("/course")
public class CourseServlet extends BaseServlet {
    //查询课程列表
    public void findCourseList(HttpServletRequest request, HttpServletResponse response){
        try {
            //1.接收参数

            //2.业务处理
            CourseService courseService = new CourseServiceImpl();
            List<Course> courseList = courseService.findCourseList();

            //3.响应结果
            //使用SimplePropertyPreFilter指定要转换的JSON字段
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,
                    "id","course_name","price","sort_num","status");
            String result = JSON.toJSONString(courseList,filter);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //根据条件查询课程信息
    public void findByCourseNameAndStatus(HttpServletRequest request, HttpServletResponse response){

        try {
            //1.接收参数
            String course_name = request.getParameter("course_name");
            String status = request.getParameter("status");

            //2.业务处理
            CourseService courseService = new CourseServiceImpl();
            List<Course> courseList = courseService.findByCourseNameAndStatus(course_name, status);

            //3.返回结果
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,
                    "id", "course_name","price","sort_num","status");
            String result = JSON.toJSONString(courseList,filter);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void findCourseById(HttpServletRequest request, HttpServletResponse response){
        try {
            //1.接收参数
            String id = request.getParameter("id");
            //2.业务处理
            CourseService courseService = new CourseServiceImpl();
            Course course = courseService.findCourseById(Integer.parseInt(id));

            //3.返回结果
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,
                    "id","course_name","brief","teacher_name", "teacher_info","preview_first_field",
                                "preview_second_field","discounts","price","price_tag","share_image_title",
                                "share_title","share_description","course_description");
            String result = JSON.toJSONString(course);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //修改课程状态的方法
    public void updateCourseStatus(HttpServletRequest request,HttpServletResponse response){
        try {
            //1.获取参数
            String id = request.getParameter("id");
            //2.业务处理
            CourseService courseService = new CourseServiceImpl();
            //3.根据id查询课程信息
            Course course = courseService.findCourseById(Integer.parseInt(id));
            //4.判断课程信息状态，进行取反（修改）操作
            int status = course.getStatus();
            if(status==0){
                //如果是0，则设置为1
                course.setStatus(1);
            }else{
                //如果是1，则设置为0
                course.setStatus(0);
            }
            //设置更新时间
            course.setUpdate_time(DateUtils.getDateFormart());
            Map<String, Integer> map = courseService.updateCourseStatus(course);

            //响应结果
            String result = JSON.toJSONString(map);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
